package tmanager;

import java.io.UnsupportedEncodingException;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Utility {

    public static String getMD5(String message) {
        MessageDigest m;
        try {
            m = MessageDigest.getInstance("MD5");
            m.update(message.getBytes("UTF8"));
        } catch (NoSuchAlgorithmException ex) {
            return "";
        } catch (UnsupportedEncodingException e) {
            return "";
        }

        byte s[] = m.digest();
        BigInteger bigInt = new BigInteger(1, s);
        return bigInt.toString(16);
    }

    public static boolean controllaPassword(String oldPwd, String newPwd, String confPwd) {
        String newMD5 = getMD5(newPwd);
        if (newPwd.equals(confPwd) && !(newMD5.equals(oldPwd)) && newPwd.length() > 6) {
            return true;
        }
        return false;
    }

    public static boolean controllaMail(String email) {
        String expression = "^[a-zA-Z0-9](\\.{0,1}[a-zA-Z0-9])*@[a-zA-Z0-9]+((\\-[a-zA-Z0-9]+)|([a-zA-Z0-9]+))+(\\.((\\-[a-zA-Z0-9]+)|([a-zA-Z0-9]{2,}))+)*(\\.[a-zA-Z]{2,3})$";
        CharSequence inputStr = email;
        Pattern pattern = Pattern.compile(expression, Pattern.CASE_INSENSITIVE);
        Matcher matcher = pattern.matcher(inputStr);
        return matcher.matches();
    }

    public static String passGen() {
        int i = 0;
        String password = "";
        while (i < 10) {
            //double x = Math.random();
            SecureRandom sr = new SecureRandom();
            float vRand = sr.nextInt(512);
            float zeroUno = vRand / 512;
            zeroUno = zeroUno * 74 + 48;
            int x = Math.round(zeroUno);
            //x = x * 74 + 48;
            if (x <= 57 || (x >= 65 && x <= 90) || x >= 97) {
                i++;
                char n = (char) x;
                password = password + n;
            }

        }
        return password;
    }
    
    public static String formatData(long timestamp) {
        SimpleDateFormat df = new SimpleDateFormat("dd.MM.yyyy 'alle' HH:mm:ss");
        return df.format(timestamp);
    }
    public static String formatData(Timestamp timestamp) {
        SimpleDateFormat df = new SimpleDateFormat("dd.MM.yyyy 'alle' HH:mm:ss");
        return df.format(timestamp);
    }
    
    public static String formatSize(long size) {
        if (size < 1024){
            return (Long.toString(size) + " Byte");
        }else if(size < 1048576){
            double sizekB=(double) size/1024;
            return String.format("%.2f kB", sizekB);
        }else{
            double sizeMB=(double) size/1048576;
            return String.format("%.2f MB", sizeMB);
        }
    }
    public static boolean isMatricolaStudente(String username){
      String expression = "^s[0-9]+$";
      CharSequence inputStr = username;
      Pattern pattern = Pattern.compile(expression);
      Matcher matcher = pattern.matcher(inputStr);
      return matcher.matches();
   }
   public static boolean isIdProfessore(String username){
      String expression = "^[0-9]+$";
      CharSequence inputStr = username;
      Pattern pattern = Pattern.compile(expression);
      Matcher matcher = pattern.matcher(inputStr);
      return matcher.matches();
   }
   public static boolean isMatricola(String matricola){
      String expression = "^[0-9]+$";
      CharSequence inputStr = matricola;
      Pattern pattern = Pattern.compile(expression);
      Matcher matcher = pattern.matcher(inputStr);
      return matcher.matches();
   }
   public static boolean isNomeOrCognome(String nome){
      String expression = "^[a-zA-Z]+(\\s*[a-zA-Z]*)*$";
      CharSequence inputStr = nome;
      Pattern pattern = Pattern.compile(expression);
      Matcher matcher = pattern.matcher(inputStr);
      return matcher.matches();
   }
}
